﻿using System.Web.Helpers;
using System.Web.Mvc;
using System.Web.WebPages;
using Microsoft.Ajax.Utilities;
using RRKAutohaus.Core;
using RRKAutohaus.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Breeze.WebApi;
using System.Net.Http;
using System.Net;

namespace RRKAutohaus.Web.Controllers
{
    [BreezeController]
    public class SenchaController : BaseApiController
    {
        //
        [HttpGet]
        public string MetaData()
        {
            return Uow.DbContext.Metadata();
        }

        public SenchaController(IUnitOfWork uow)
        {
            this.Uow = uow;
        }

        [HttpGet]
        public List<Car> GetFeaturedCars()
        {
            var cars = Uow.Cars.GetFeaturedCars();
            return cars.ToList();
        }

        [HttpGet]
        public IQueryable<Car> GetAllCars(string carType)
        {
            var cars = Uow.Cars.GetAll().Where(c => c.CarType.ToLower() == carType.ToLower());
            
            return cars;
        }

        [HttpGet]
        public IQueryable<Car> GetAllCars(string carModel, string carType)
        {
            var cars = Uow.Cars.GetAll().Where(c => c.CarType.ToLower() == carType.ToLower()
                                && c.BrandName.ToLower() == carModel.ToLower());

            return cars;
        }

        [HttpPost]
        public HttpResponseMessage UpdateCar(Car car)
        {
            Uow.Cars.UpdateEntity(car);
            Uow.Commit();
            var response = Request.CreateResponse<Car>(HttpStatusCode.Created, car);


            string uri = Url.Link("DefaultApi", new { id = car.Id });
            response.Headers.Location = new Uri(uri);
            return response;
        }
    }
}
